package com.woniuxy.qiantai.controller;

import com.woniuxy.qiantai.entity.Order;
import com.woniuxy.qiantai.service.BusinessService;
import com.woniuxy.qiantai.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    BusinessService businessService;

    //获取商家订单
    @RequestMapping("index")
        public String index(Integer BusinessId){

            return"order";
        }

        //用户支付
    @RequestMapping("pay")
    @ResponseBody
    public String pay(Integer orderId, Integer states){
        System.out.println("state="+states);
       /* orderService.updateOrderState(orderId,states);
        System.out.println("更新后orderId:"+orderId+"更新后state:"+states);
        orderService.expenditure(orderId);*/
        //判断订单是否存在
        Order order = orderService.selectOrderInfo(orderId);
        if (order!=null){
            orderService.updateOrderState(orderId,states);
            System.out.println("更新后orderId:"+orderId+"更新后state:"+states);
            orderService.expenditure(orderId);
        }
        //支付后给商家发送订单邮件
        orderService.sendOrderEmail(orderId);
        System.out.println("给商家发送订单邮件");
        //
        return "ok";
    }

    //用户下单延迟支付
    @RequestMapping("createOrder")
    @ResponseBody
    public String createOrder(Integer orderId){
        orderService.sendMessageToDelayQueue(orderId);
        System.out.println("延迟更新的orderId:"+orderId);
        return "ok";
    }

    //用户手动确认收获
    @RequestMapping("acknowledgement")
    @ResponseBody
    public String acknowledgement(Integer orderId, Integer states){

        orderService.updateOrderState(orderId,states);
        System.out.println("改变状态为确认收货");
        return"ok";

    }

}

